#include "s_user_ds18b20.h"

#include "h_stm32_hal_defines.h"
#include "h_stm32_hal_timer.h"

#include "s_ds18b20.h"

static ds18b20_t ds1, ds2, ds3;


#define DS18B20_0_PORT                                    GPIOC
#define DS18B20_0_PIN                                     GPIO_PIN_0

#define DS18B20_1_PORT                                    GPIOC
#define DS18B20_1_PIN                                     GPIO_PIN_1

#define DS18B20_2_PORT                                    GPIOC
#define DS18B20_2_PIN                                     GPIO_PIN_4
//////////////////////////////////////////////////////////////////////////////////
void s_ds18b20_data_line_out_init_1(void)
{
	GPIO_InitTypeDef ds18b20_GPIO_InitStruct = {0};

	ds18b20_GPIO_InitStruct.Pin = DS18B20_0_PIN;
	ds18b20_GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
	ds18b20_GPIO_InitStruct.Pull = GPIO_NOPULL;
	ds18b20_GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
	HAL_GPIO_Init(DS18B20_0_PORT, &ds18b20_GPIO_InitStruct);
}

void s_ds18b20_data_line_out_high_1(void)
{
	DS18B20_0_PORT->BSRR = DS18B20_0_PIN;
}

void s_ds18b20_data_line_out_low_1(void)
{
	DS18B20_0_PORT->BRR = DS18B20_0_PIN;
}

void s_ds18b20_data_line_in_init_1(void)
{
	GPIO_InitTypeDef ds18b20_GPIO_InitStruct = {0};

	ds18b20_GPIO_InitStruct.Pin = DS18B20_0_PIN;
	ds18b20_GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
	ds18b20_GPIO_InitStruct.Pull = GPIO_NOPULL;
	ds18b20_GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
	HAL_GPIO_Init(DS18B20_0_PORT, &ds18b20_GPIO_InitStruct);
}

signed char s_ds18b20_data_line_read_1(void)
{
	return HAL_GPIO_ReadPin(DS18B20_0_PORT, DS18B20_0_PIN);
}


void s_ds18b20_1_init(void)
{
    ds1.ds18b20_out_init = &s_ds18b20_data_line_out_init_1;
    ds1.ds18b20_out_high = &s_ds18b20_data_line_out_high_1;
    ds1.ds18b20_out_low = &s_ds18b20_data_line_out_low_1;
    ds1.ds18b20_in_init = &s_ds18b20_data_line_in_init_1;
    ds1.ds18b20_read = &s_ds18b20_data_line_read_1;
    ds1.ds18b20_delay_us = &delay_us;
}

//////////////////////////////////////////////////////////////////////////////////
void s_ds18b20_data_line_out_init_2(void)
{
	GPIO_InitTypeDef ds18b20_GPIO_InitStruct = {0};

	ds18b20_GPIO_InitStruct.Pin = DS18B20_1_PIN;
	ds18b20_GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
	ds18b20_GPIO_InitStruct.Pull = GPIO_NOPULL;
	ds18b20_GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
	HAL_GPIO_Init(DS18B20_1_PORT, &ds18b20_GPIO_InitStruct);
}

void s_ds18b20_data_line_out_high_2(void)
{
	DS18B20_1_PORT->BSRR = DS18B20_1_PIN;
}

void s_ds18b20_data_line_out_low_2(void)
{
	DS18B20_1_PORT->BRR = DS18B20_1_PIN;
}

void s_ds18b20_data_line_in_init_2(void)
{
	GPIO_InitTypeDef ds18b20_GPIO_InitStruct = {0};

	ds18b20_GPIO_InitStruct.Pin = DS18B20_1_PIN;
	ds18b20_GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
	ds18b20_GPIO_InitStruct.Pull = GPIO_NOPULL;
	ds18b20_GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
	HAL_GPIO_Init(DS18B20_1_PORT, &ds18b20_GPIO_InitStruct);
}

signed char s_ds18b20_data_line_read_2(void)
{
	return HAL_GPIO_ReadPin(DS18B20_1_PORT, DS18B20_1_PIN);
}


void s_ds18b20_2_init(void)
{
    ds2.ds18b20_out_init = &s_ds18b20_data_line_out_init_2;
    ds2.ds18b20_out_high = &s_ds18b20_data_line_out_high_2;
    ds2.ds18b20_out_low = &s_ds18b20_data_line_out_low_2;
    ds2.ds18b20_in_init = &s_ds18b20_data_line_in_init_2;
    ds2.ds18b20_read = &s_ds18b20_data_line_read_2;
    ds2.ds18b20_delay_us = &delay_us;
}

//////////////////////////////////////////////////////////////////////////////////
void s_ds18b20_data_line_out_init_3(void)
{
	GPIO_InitTypeDef ds18b20_GPIO_InitStruct = {0};

	ds18b20_GPIO_InitStruct.Pin = DS18B20_2_PIN;
	ds18b20_GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
	ds18b20_GPIO_InitStruct.Pull = GPIO_NOPULL;
	ds18b20_GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
	HAL_GPIO_Init(DS18B20_2_PORT, &ds18b20_GPIO_InitStruct);
}

void s_ds18b20_data_line_out_high_3(void)
{
	DS18B20_2_PORT->BSRR = DS18B20_2_PIN;
}

void s_ds18b20_data_line_out_low_3(void)
{
	DS18B20_2_PORT->BRR = DS18B20_2_PIN;
}

void s_ds18b20_data_line_in_init_3(void)
{
	GPIO_InitTypeDef ds18b20_GPIO_InitStruct = {0};

	ds18b20_GPIO_InitStruct.Pin = DS18B20_2_PIN;
	ds18b20_GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
	ds18b20_GPIO_InitStruct.Pull = GPIO_NOPULL;
	ds18b20_GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
	HAL_GPIO_Init(DS18B20_2_PORT, &ds18b20_GPIO_InitStruct);
}

signed char s_ds18b20_data_line_read_3(void)
{
	return HAL_GPIO_ReadPin(DS18B20_2_PORT, DS18B20_2_PIN);
}


void s_ds18b20_3_init(void)
{
    ds3.ds18b20_out_init = &s_ds18b20_data_line_out_init_3;
    ds3.ds18b20_out_high = &s_ds18b20_data_line_out_high_3;
    ds3.ds18b20_out_low = &s_ds18b20_data_line_out_low_3;
    ds3.ds18b20_in_init = &s_ds18b20_data_line_in_init_3;
    ds3.ds18b20_read = &s_ds18b20_data_line_read_3;
    ds3.ds18b20_delay_us = &delay_us;
}

//////////////////////////////////////////////////////////////////////////////////


void s_ds18b20_1_get_temperature(float *temp)
{
    s_ds18b20_get_temperature(&ds1, temp);
}

void s_ds18b20_2_get_temperature(float *temp)
{
    s_ds18b20_get_temperature(&ds2, temp);
}

void s_ds18b20_3_get_temperature(float *temp)
{
    s_ds18b20_get_temperature(&ds3, temp);
}